/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bccomp;

import java.util.HashMap;
import java.util.Scanner;

/**
 *
 * @author adam
 */
public class LabelReduction {

    private String input;
    private String output="";
    private int bytes;
    private HashMap<String, Integer> map = new HashMap<String, Integer>();

    public LabelReduction(String input) {
        this.input = input;
        output="";
        bytes=0;
    }

    public String reduce() {
        Scanner sc = new Scanner(input);
        String token;
        bytes=0;
        output="";
        while (sc.hasNext()) {
            if(sc.hasNextInt()){
                int a=sc.nextInt();
                output=getOutput().concat(a+" ");
                bytes=bytes+4;
                continue;
            }
            
            token = sc.next();
            if (token.equals("method")) {
                String methodName=sc.next();
                if(methodName.equals("main")){
                    output=getOutput().concat("main ");
                    bytes++;
                }else{
                map.put(methodName,(Integer)bytes);
                }
            } else if (token.equals("label")) {
                String labelName=sc.next().replaceAll(":", "");
                map.put(labelName,(Integer)bytes);
            } else if(token.startsWith("\"")){
                String s="";
                int a=0;
                while(!((token.endsWith("\""))&& (a>0 || token.length()>1))){
                    s=s.concat(token+" ");
                    a++;
                    token=sc.next();
                }
                
                s=s.concat(token);
                
                bytes=bytes+s.replaceAll("\"", "").getBytes().length;
                bytes=bytes+4;
                output=getOutput().concat(s+" ");
            }else if(token.equals("goto")||token.equals("if")||token.equals("invoke")){
                bytes=bytes+4;
                output=getOutput().concat(token+" ");
            }else{
                bytes++;
                output=getOutput().concat(token+" ");
            }



        }

        return getOutput();
    }
    
    public String reduce2(){
        String oup="";
        
         Scanner sc = new Scanner(output);
        String token;
        while (sc.hasNext()) {
            token=sc.next();
            if(token.equals("goto")){
                oup=oup.concat(token+" ");
                String lab=sc.next();
                Integer position=map.get(lab);
                oup=oup.concat(position+" "); 
            } else if(token.equals("if")){
                oup=oup.concat(token+" "+sc.next()+" ");
                String lab=sc.next();
                Integer position=map.get(lab);
                oup=oup.concat(position+" "); 
            }else if(token.equals("invoke")){
                oup=oup.concat(token+" ");
                String lab=sc.next();
                Integer position=map.get(lab+"()");
                oup=oup.concat(position+" "); 
            }else{
                oup=oup.concat(token+" ");
            }
            
            
        }
                
        return oup;
        
    }

    /**
     * @return the output
     */
    public String getOutput() {
        return output;
    }

    /**
     * @return the map
     */
    public HashMap<String, Integer> getMap() {
        return map;
    }
}
